package com.song.util.excel;

import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import cn.afterturn.easypoi.excel.export.ExcelExportService;
import cn.afterturn.easypoi.util.PoiMergeCellUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;

import java.util.Collection;
import java.util.List;

public class CustomExcelExportService extends ExcelExportService {
    @Override
    protected int createHeaderAndTitle(ExportParams exportParams, Sheet sheet, Workbook workbook, List<ExcelExportEntity> excelParams) {
        int rowIndex = 0;
        int fieldLength = this.getFieldLength(excelParams);
        if (exportParams.getTitle() != null) {
            rowIndex += this.createTitle2Row(exportParams, sheet, workbook, fieldLength);
        }
        int rows = this.getRowNums(excelParams, true);
        this.createHeaderRow(exportParams, sheet, workbook, rowIndex, excelParams, 0);
        rowIndex += rows;
        if (exportParams.isFixedTitle()) {
            sheet.createFreezePane(0, rowIndex, 0, rowIndex);
        }
        return rowIndex;
    }

    private int createHeaderRow(ExportParams title, Sheet sheet, Workbook workbook, int index, List<ExcelExportEntity> excelParams, int cellIndex) {
        Row row = sheet.getRow(index) == null ? sheet.createRow(index) : sheet.getRow(index);
        int rows = this.getRowNums(excelParams, true);
        row.setHeight(title.getHeaderHeight());
        Row listRow = null;
        if (rows >= 2) {
            listRow = sheet.getRow(index + 1);
            if (listRow == null) {
                listRow = sheet.createRow(index + 1);
                listRow.setHeight(title.getHeaderHeight());
            }
        }

        int groupCellLength = 0;
        CellStyle titleStyle = this.getExcelExportStyler().getTitleStyle(title.getColor());
        int i = 0;

        for(int exportFieldTitleSize = excelParams.size(); i < exportFieldTitleSize; ++i) {
            ExcelExportEntity entity = (ExcelExportEntity)excelParams.get(i);
            if (StringUtils.isBlank(entity.getGroupName()) || i == 0 || !entity.getGroupName().equals(((ExcelExportEntity)excelParams.get(i - 1)).getGroupName())) {
                if (groupCellLength > 1) {
                    sheet.addMergedRegion(new CellRangeAddress(index, index, cellIndex - groupCellLength, cellIndex - 1));
                }

                groupCellLength = 0;
            }

            if (StringUtils.isNotBlank(entity.getGroupName())) {
                this.createStringCell(row, cellIndex, entity.getGroupName(), titleStyle, entity);
                this.createStringCell(listRow, cellIndex, entity.getName(), titleStyle, entity);
                ++groupCellLength;
            } else if (StringUtils.isNotBlank(entity.getName())) {
                this.createStringCell(row, cellIndex, entity.getName(), titleStyle, entity);
            }

            if (entity.getList() != null) {
                int tempCellIndex = cellIndex;
                List<ExcelExportEntity> sTitel = entity.getList();
                int fieldLength = this.getFieldLength(sTitel);
                for (int j = tempCellIndex; j <= tempCellIndex + fieldLength; j++) {
                    this.createStringCell(row, j, entity.getName(), titleStyle, entity);
                }
                cellIndex = this.createHeaderRow(title, sheet, workbook, rows == 1 ? index : index + 1, entity.getList(), cellIndex);
                if (StringUtils.isNotBlank(entity.getName()) && sTitel.size() > 1) {
                    PoiMergeCellUtil.addMergedRegion(sheet, index, index, tempCellIndex, tempCellIndex + fieldLength);
                }

                --cellIndex;
            } else if (rows > 1 && StringUtils.isBlank(entity.getGroupName())) {
                for (int j = index; j <= index + rows - 1; j++) {
                    Row tempRow = sheet.getRow(j) == null ? sheet.createRow(j) : sheet.getRow(j);
                    this.createStringCell(tempRow, cellIndex, entity.getName(), titleStyle, entity);
                }
                PoiMergeCellUtil.addMergedRegion(sheet, index, index + rows - 1, cellIndex, cellIndex);
            }

            ++cellIndex;
        }

        if (groupCellLength > 1) {
            PoiMergeCellUtil.addMergedRegion(sheet, index, index, cellIndex - groupCellLength, cellIndex - 1);
        }

        return cellIndex;
    }

    public boolean isEmpty(Collection collection){
        if (collection == null || collection.size() == 0){
            return true;
        }
        return false;
    }

    public boolean isNotEmpty(Collection collection){
        return !isEmpty(collection);
    }
}
